package com.damuzee.system.admin.quarzt;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.damuzee.system.admin.domain.AgentAcct;
import com.damuzee.system.admin.domain.TipMsg;
import com.damuzee.system.admin.service.AgentAcctService;
import com.damuzee.system.admin.service.TipMsgService;

/**
 * 每天检查三天后是否有待办任务
 * 
 * 1、检查代理记账是否截止收费、签约、合同到期（第一天灰色提示、第二天黄色提示、第三天红色提示）
 * 2、
 * 
 * 
 * @author karka.w
 * 2013-9-16
 *
 */
public class QuartzJob {
	private static final Logger log = LoggerFactory.getLogger(QuartzJob.class);
	@Resource
	private AgentAcctService agentAcctService ;
	@Resource 
	private TipMsgService tipMsgService ;
	
	public void run() {
		log.info("-------------定时任务启动------------------");
		
		log.info("-------------清理历史消息------------------");
		tipMsgService.deleteAllMsg();
		
		log.info("-------------计算历史提醒------------------");
		AgentAcct acct = new AgentAcct();
		acct.setCheckeState(0);
		
		List<AgentAcct> list = agentAcctService.getAgentAcctList(acct);
		for (AgentAcct a : list) {
			/**计算收费截至日期**/
			String stopDate =  a.getChargeStopDate();
			if (stopDate != null) {
				Long day = getDaysBetween(stopDate,new Date());
				if (day < 3 && day > -3) {
					TipMsg tipMsg = new TipMsg();
					tipMsg.setCheckState(0);
					tipMsg.setCheckUser(a.getCheckUser());
					
					if(day>0){
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]还有【"+day+"】天到收费日期");
					}else if(day==0){
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]收费日期今天到期");
					}else{
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]收费到期已经超过【"+day+"】天");
					}
					tipMsg.setTitle("合同收费");
					tipMsg.setOprDate(a.getChargeStopDate());
					tipMsg.setOprDay(Integer.parseInt(day.toString()));
					tipMsgService.saveTipMsg(tipMsg);
				}
			}
			
			
		   stopDate =  a.getSignStopDate();
		   if (stopDate != null) {
			  Long day = getDaysBetween(stopDate,new Date());
				if (day < 3 && day > -3) {
					TipMsg tipMsg = new TipMsg();
					tipMsg.setCheckState(0);
					tipMsg.setCheckUser(a.getCheckUser());
					if(day>0){
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]还有【"+day+"】天到签约日期");
					}else if(day==0){
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]签约日期今天到期");
					}else{
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]签约到期已经超过【"+day+"】天");
					}
					tipMsg.setTitle("合同签约");
					tipMsg.setOprDate(a.getChargeStopDate());
					tipMsg.setOprDay(Integer.parseInt(day.toString()));
					tipMsgService.saveTipMsg(tipMsg);
				}
		   }
		   
		   stopDate =  a.getDealEndDate();
		   if (stopDate != null) {
			  Long day = getDaysBetween(stopDate,new Date());
				if (day < 3 && day > -3) {
					TipMsg tipMsg = new TipMsg();
					tipMsg.setCheckState(0);
					tipMsg.setCheckUser(a.getCheckUser());
					if(day>0){
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]还有【"+day+"】天到期");
					}else if(day==0){
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]期限今天到期");
					}else{
						tipMsg.setContext("客户为【"+a.getCustName()+"】的合同["+a.getAcctId()+"]到期已经超过【"+day+"】天");
					}
					tipMsg.setTitle("合同到期");
					tipMsg.setOprDate(a.getChargeStopDate());
					tipMsg.setOprDay(Integer.parseInt(day.toString()));
					tipMsgService.saveTipMsg(tipMsg);
				}
		   }
			
		}
		
	}
	
	public   Long getDaysBetween(String start, Date endDate) {  
		try {
	        Calendar fromCalendar = Calendar.getInstance();  
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	        Date startDate  = sdf.parse(start);
		
	        fromCalendar.setTime(startDate);  
	        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);  
	        fromCalendar.set(Calendar.MINUTE, 0);  
	        fromCalendar.set(Calendar.SECOND, 0);  
	        fromCalendar.set(Calendar.MILLISECOND, 0);  
	  
	        
	        Calendar toCalendar = Calendar.getInstance();  
	        toCalendar.setTime(endDate);  
	        toCalendar.set(Calendar.HOUR_OF_DAY, 0);  
	        toCalendar.set(Calendar.MINUTE, 0);  
	        toCalendar.set(Calendar.SECOND, 0);  
	        toCalendar.set(Calendar.MILLISECOND, 0);  
	  
	        return ( fromCalendar.getTime().getTime() - toCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24); 
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null ;
	}
}